package com.mindskip.xzs.repository;

import com.mindskip.xzs.domain.other.KeyValue;
import com.mindskip.xzs.domain.User;
import com.mindskip.xzs.repository.base.MyMapper;
import com.mindskip.xzs.viewmodel.admin.user.UserPageRequestVM;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @author 武汉思维跳跃科技有限公司
 */

@Mapper
@Repository
public interface UserMapper extends MyMapper<User> {


//    int deleteByPrimaryKey(Integer id);
//
//    int insert(User record);
//
//    int insertSelective(User record);
//
//    User selectByPrimaryKey(Integer id);
//
//    int updateByPrimaryKeySelective(User record);
//
//    int updateByPrimaryKey(User record);

    String queryParams = " id, user_uuid userUuid, user_name userName, password, real_name realName, age, sex, " +
            "birth_day birthDay, user_level userLevel, phone, role, status, image_path imagePath, " +
            "create_time createTime, modify_time modifyTime, last_active_time lastActiveTime, deleted, " +
            "wx_open_id wxOpenId ";

    /**
     * getAllUser
     *
     * @return List<User>
     */
//    List<User> getAllUser();

    /**
     * getUserById
     *
     * @param id id
     * @return User
     */
//    User getUserById(Integer id);

    /**
     * getUserByUserName
     *
     * @param username username
     * @return User
     */
//    User getUserByUserName(String username);

    /**
     * getUserByUserNamePwd
     *
     * @param username username
     * @param pwd      pwd
     * @return User
     */
//    User getUserByUserNamePwd(@Param("username") String username, @Param("pwd") String pwd);

    /**
     * getUserByUuid
     *
     * @param uuid uuid
     * @return User
     */
    User getUserByUuid(String uuid);

    /**
     * userPageList
     *
     * @param map userPageList
     * @return List<User>
     */
    @Select("<script>" +
            "SELECT\n" + queryParams +
            "    FROM t_user\n" +
            "    <where>\n" +
            "        and deleted=0\n" +
            "      <if test=\"name != null and name != ''\">\n" +
            "        and  real_name like concat('%',#{name},'%')\n" +
            "      </if>\n" +
            "    </where>\n" +
            "    ORDER BY id\n" +
            "    <if test=\"offset != null and limit != null \">\n" +
            "      <bind name=\"patternAdd\" value=\"limit*offset\"/>\n" +
            "      limit #{limit} OFFSET #{offset}\n" +
            "    </if>" +
            "</script>")
    List<User> userPageList(Map<String, Object> map);


    /**
     * userPageCount
     *
     * @param map map
     * @return Integer
     */
    @Select("<script>" +
            "select count(*) from t_user\n" +
            "    <where>\n" +
            "        and deleted=0\n" +
            "      <if test=\"name != null and name != ''\">\n" +
            "        and real_name like concat('%', #{name}, '%')\n" +
            "      </if>\n" +
            "    </where>" +
            "</script>")
    Integer userPageCount(Map<String, Object> map);


    /**
     * @param requestVM requestVM
     * @return List<User>
     */
    @Select("<script>" +
            "SELECT\n" + queryParams +
            "    FROM t_user\n" +
            "    <where>\n" +
            "        and deleted=0\n" +
            "      <if test=\"userName != null and userName != ''\">\n" +
            "        and user_name like concat('%',#{userName},'%')\n" +
            "      </if>\n" +
            "      <if test=\"role != null \">\n" +
            "        and role= #{role}\n" +
            "      </if>\n" +
            "</where>" +
            "</script>")
    List<User> userPage(UserPageRequestVM requestVM);


    /**
     * insertUser
     *
     * @param user user
     */
//    void insertUser(User user);

    /**
     * insertUsers
     *
     * @param users users
     */
//    void insertUsers(List<User> users);

    /**
     * updateUser
     *
     * @param user user
     */
//    void updateUser(User user);

    /**
     * updateUsersAge
     *
     * @param map map
     */
    @Update("<script>" +
            "update t_user set age = #{age} where id in\n" +
            "    <foreach item=\"id\" collection=\"idslist\" open=\"(\" separator=\",\"\n" +
            "             close=\")\">\n" +
            "      #{id}\n" +
            "    </foreach>" +
            "</script>")
    void updateUsersAge(Map<String, Object> map);

    /**
     * deleteUsersByIds
     *
     * @param ids ids
     */
//    void deleteUsersByIds(List<Integer> ids);

    /**
     * insertUserSql
     *
     * @param user user
     */
//    void insertUserSql(User user);

//    Integer selectAllCount();

    @Select("SELECT  id as value,user_name as name\n" +
            "\t\tfrom t_user\n" +
            "\t\twhere deleted=0  and user_name like concat('%',#{userName},'%')\n" +
            "\t\tlimit 5")
    List<KeyValue> selectByUserName(@Param("userName") String userName);

//    List<User> selectByIds(List<Integer> ids);

    @Select("select\n" + queryParams +
            "    from t_user\n" +
            "    where deleted=0  and  wx_open_id = #{wxOpenId} limit 1")
    User selectByWxOpenId(@Param("wxOpenId") String wxOpenId);
}
